//
//  TJEmotionView.swift
//  表情键盘
//
//  Created by 唐后能 on 16/4/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
// 定义代理协议.
protocol TJEmotionViewCellDelegate:NSObjectProtocol {

    // 表情按钮的点击监听事件.
    func emotionViewCellEmotionBtnClicked(emotion:TJEmotion)
    // 删除按钮点击事件代理方法.
    func emoticonViewCellDeleteButtonClicked(cell: TJEmotionViewCell)
}

class TJEmotionViewCell: UICollectionViewCell {

    // 定义代理属性.
    weak var delegate:TJEmotionViewCellDelegate?
    
    // 定义数组,用来存放表情按钮.
    var emotionBtns:[TJEmotionBtn] = []
    
    // 分页表情模型数组,
    var emotionPackage:[TJEmotion]?{
    
        didSet {
            
            // 一开始隐藏全部按钮.
            for btn in emotionBtns {
            
                btn.hidden = true
            }
        
            /*
            *   1,只要emotionPackage中有emotion模型就会走下面for循环,即使是空模型(模型数据为空)也会走-->读取沙盒数据失败时,有创建空模型;
            *   2,所以要在for循环中判断emotion中的emoji属性或pngfullPath是否有值,才去显示button,传递数据;
                3,最好在给button赋值时候也加上一个else数据为空时,把title和image都设置为nil来解决cell复用问题,但按钮还是存在,只不过赋值为空罢了.
            *
            */
            for (index,emotion) in emotionPackage!.enumerate() {
                //当emotion中的emoji属性或pngfullPath有值,才去显示button,传递数据.
                if emotion.emoji != nil || emotion.fullPngPath != nil {
                
                    // 找到emotion模型对应的按钮.
                    let btn = emotionBtns[index]
                    btn.hidden = false
                    // 将表情模型传递给按钮,让他自己赋值.
                    btn.emotion = emotion
                }
            }
        }
    }
    var indexPath:NSIndexPath?{
    
        didSet {
        
//            debugLabel.text = "第\(indexPath!.section)组,  第\(indexPath!.item)页"
            recentLabel.hidden = indexPath?.section != 0
        }
    }
    
    // MAEK - 初始化方法.
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI ()
    
    }
    // MARK - 添加子控件.
    private func prepareUI(){
        // 测试label.
        addtestLabel()
        // 添加20个表情按钮.
        addEmotionBtn()
        // 添加删除按钮
        contentView.addSubview(deleteBtn)
        // 添加最近使用表情.
        contentView.addSubview(recentLabel)
    }
    
    // 添加20个表情按钮.
    private func addEmotionBtn(){
    
        for _ in 0..<numberOfPage {
            
            let btn = TJEmotionBtn()
            // 设置btn的大小.
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
//            btn.backgroundColor = UIColor.randomColor()
            // 添加按钮的点击事件.
            btn.addTarget(self, action: Selector("emotionBtnClicked:"), forControlEvents: UIControlEvents.TouchUpInside)
            
            // 添加到按钮数组中,下面直接遍历出来进行布局.
            emotionBtns.append(btn)
            
            // 将表情按钮添加到contentView中.
            contentView.addSubview(btn)
        }
    }

    
    // MARK: - 表情按钮的点击事件.
    @objc private func emotionBtnClicked(btn:TJEmotionBtn) {
    
        // 获取点击表情的序号.
        let btnIndex = emotionBtns.indexOf(btn)
        // 去除分页模型中对应的表情模型.
        let emotion = emotionPackage![btnIndex!]
        // 调用代理方法.
        delegate?.emotionViewCellEmotionBtnClicked(emotion)
    }
    @objc private func didClickDeleteButton (){
    
        // 调用代理方法.
        delegate?.emoticonViewCellDeleteButtonClicked(self)
    }
    
    // MARK - 布局子控件.
    override func layoutSubviews() {
        super.layoutSubviews()
        layoutEmotionBtn()
        
    }
    
    // 布局cell中的按钮.
    private func layoutEmotionBtn(){
    
        let marginLR:CGFloat = 5
        let marginB:CGFloat = 25
        
        let btnW = (contentView.bounds.width - 2*marginLR) / CGFloat(culomNumberOfPage)
        
        let btnH = (contentView.bounds.height - marginB) / CGFloat(rowNumberOfPage)
        
        // 布局20个表情按钮.
        for (index,btn) in emotionBtns.enumerate() {
            
            let btnX = marginLR + CGFloat(index%culomNumberOfPage)*btnW
            let btnY = CGFloat(index/culomNumberOfPage)*btnH
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
        }
        
        // 布局删除按钮.
        let deleteBtnX = marginLR + CGFloat(numberOfPage%culomNumberOfPage)*btnW
        let deleteBtnY = CGFloat(numberOfPage/culomNumberOfPage)*btnH
        deleteBtn.frame = CGRect(x: deleteBtnX, y: deleteBtnY, width: btnW, height: btnH)
        
        // 布局最近使用label.
        let recentLblX = (contentView.bounds.width - recentLabel.bounds.width)*0.5
        let recentLblY = contentView.bounds.height - recentLabel.bounds.height - 5
        recentLabel.frame.origin = CGPoint(x: recentLblX, y: recentLblY)
    }
    
    
    
    // 懒加载删除按钮.
    private lazy var deleteBtn:UIButton = {
    
        let btn = UIButton()
        // 设置.
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    // 最近使用label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(12)
        
        label.text = "最近使用表情"
        label.sizeToFit() //只能设置大小,默认位置为父控件的左上角
        
        return label
    }()
    
    // TODO:测试label
    private func addtestLabel(){
    
        contentView.addSubview(debugLabel)
        debugLabel.frame = bounds
    }
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.redColor()
        
        return label
    }()


}
